<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Widgets extends MX_Controller 
{
	public function __construct()
	{
		parent::__construct();	
		$this->display->set_theme('cms');
		
	}
	
	public function index()
	{
	    //permission('cms', 'widget_list');
		$this->load->library('widget');
		$data['installed_widgets'] = $this->widget->get_installed_widgets();
		$data['available_widgets'] = $this->widget->get_available_widgets();
		
		$this->twiggy->title()->append('Dashboard');
		$this->display->template('widget_list', $data);
	}
	
	function edit($id)
	{
        //permission('cms', 'widget_edit');
		$widget = $this->widget->get(array('id' => $id));	
	}
    
    function delete($id)
    {
        //permission('cms', 'widget_edit');        
        $this->load->library('widget');
        if(!ctype_digit($id))
        {
            flash_message('error', 'Invalid request');           
        }
        else
        {               
            $this->widget->uninstall($id);
            flash_message('success', 'Widget removed');
        }
        cms_redirect('widgets');
    }
    
	function add()
	{
	    //permission('cms', 'widget_add');
	    print_r($this->input->post());
        $this->load->library('widget');
        $this->load->library('form_validation');    
        $this->form_validation->set_rules('name', 'Name', 'required|is_unique[widgets.name]|max_length[20]'); 
        $this->form_validation->set_rules('type', 'Type', 'required|callback_validate_type');
        $valid = $this->form_validation->run();             
        if($valid)
        {                   
            $file_name = $this->input->post('type');
            $name = $this->input->post('name');
            $widget_id = $this->widget->install($file_name, $name);
            if($widget_id !== FALSE)
            {
               // $this->edit($id);
            }
            else
            {
                echo 'widget not installed';
            }
            flash_message('success', 'Widget added');
        }
        else
        {
            flash_message('error', validation_errors());
        }
        cms_redirect('widgets');      
	}
    
    
    
    
    public function validate_type($type)
    {
        $types = $this->widget->get_available_widgets();
        if(!in_array($type, $types))
            return FALSE;
       
        return FALSE;
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */